// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
/* base style */
import '../static/lib/css/base.scss'

/*vant ui */
import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

/* @desc:httpInterceptor */
import {request,response} from './service/httpInterceptor'
axios.interceptors.request.use(request)
axios.interceptors.response.use(response);

/*filter*/
import filter from './plugin/filter'
Vue.use(filter)

/*method*/
import pluginMethod from './plugin/method'
Vue.use(pluginMethod)
/*component*/
import component from './plugin/component'
Vue.use(component)

/* fastclick */ 
import FastClick from 'fastclick'
FastClick.attach(document.body)

import VueLazyLoad from 'vue-lazyload'
Vue.use(VueLazyLoad,{
    error:'./../static/lib/img/error.png',
    loading:'./../static/lib/img/loading.png'
})
// vuex
import store from './vuex/index'
Vue.config.productionTip = false
function getParam(name){
    let urlS = window.location.href;
    let theRequest = new Object();
    if (urlS.indexOf('?') != -1) {
        let strUrl = urlS.split('?'); 
        let strId=strUrl[1]?strUrl[1].split('&'):[]; 
        for (let i = 0; i < strId.length; i++) {
            if (strId[i].split('=')[0] == name) {
                return unescape(strId[i].split('=')[1]); 
            }
        }
        return '';
    }
} 
import * as api from './api/api'   

router.beforeEach(function (to, from, next) { 
    if(sessionStorage.getItem('userInfo')){
        next(); 
    }else{
        if(getParam('code')){
            api.wxCallback({code:getParam('code')}).then(res=>{
                if(res.data.code==200){
                    sessionStorage.setItem('userInfo',JSON.stringify(res.data.data))
                    sessionStorage.setItem('token',res.data.data.token)
                    if(getParam('state')){
                        let stateItem=getParam('state').split(',')
                        console.log(stateItem)
                        let newUrlCode=''
                        stateItem.forEach((item,index)=>{
                            newUrlCode=newUrlCode?newUrlCode+'&'+item : item;
                        })
                        console.log(newUrlCode);
                        window.location.href=`${decodeURIComponent(window.location.href).split('?')[0]}?${newUrlCode}`
                    }
                }
            })  
        }else{  
            if (window.location.href.indexOf('?') != -1) { 
                var strArr = window.location.href.split('?');
                let stateCode=strArr[1]?strArr[1].split('&'):[];  
                let stateItem=''; 
                stateCode.forEach((item,index)=>{ 
                    stateItem=stateItem?stateItem+','+item:item; 
                })  
                window.location.href=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx97a140fa72d60e26&redirect_uri=${encodeURIComponent(strArr[0])}&response_type=code&scope=snsapi_userinfo&state=${stateItem}#wechat_redirect`;  

                // window.location.href=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx97a140fa72d60e26&redirect_uri=http://api.chinahycc.com&response_type=code&scope=snsapi_userinfo&state=${stateItem}#wechat_redirect`;  
            } else{
                window.location.href=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx97a140fa72d60e26&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`; 
            } 
        }  
    }
    if (to.meta && to.meta.title){
        document.title = to.meta.title;
    }  
    
});
//encodeURIComponent //编码
//decodeURIComponent //解码
// wx97a140fa72d60e26
// import * as wxApi from './service/wxApiList' 
// window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx97a140fa72d60e26&redirect_uri=http://api.chinahycc.com/test&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
